package com.beaven.imserver.net.service;

import com.beaven.imserver.net.bean.api.base.PushModel;
import com.beaven.imserver.net.bean.api.base.ResponseModel;
import com.beaven.imserver.net.bean.api.group.GroupCreateModel;
import com.beaven.imserver.net.bean.api.group.GroupMemberCreateModel;
import com.beaven.imserver.net.bean.api.group.UpdateGroupModel;
import com.beaven.imserver.net.bean.card.GroupCard;
import com.beaven.imserver.net.bean.card.GroupMemberCard;
import com.beaven.imserver.net.bean.db.Group;
import com.beaven.imserver.net.bean.db.GroupMember;
import com.beaven.imserver.net.bean.db.User;
import com.beaven.imserver.net.factory.GroupFactory;
import com.beaven.imserver.net.factory.PushFactory;
import com.beaven.imserver.net.factory.UserFactory;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * author: Beaven
 * time: 2017/6/1 17:32
 */
@Path("/group")
public class GroupService extends BaseService {

  // 创建群
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<GroupCard> registerGroup(GroupCreateModel model) {
    if (!GroupCreateModel.check(model)) {
      return ResponseModel.buildParameterError();
    }
    User user = getSelf();
    Group group = GroupFactory.findByName(model.getName(), user);
    if (group != null) {
      return ResponseModel.buildHaveNameError();
    }
    group = GroupFactory.createGroup(model.getName(), user);
    if (group != null) {
      // 添加当前成员进入群
      GroupMember groupMember = GroupFactory.createGroupMember(group, user, true);
      if (groupMember != null) {
        GroupCard groupCard = new GroupCard(group, groupMember);
        return ResponseModel.buildOk(groupCard);
      }
    }
    return ResponseModel.buildRegisterError();
  }

  //删除群
  @DELETE
  @Path("/{groupId}")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel deleteGroup(@PathParam("groupId") String groupId) {
    if (Strings.isNullOrEmpty(groupId)) {
      return ResponseModel.buildParameterError();
    }
    Group group = GroupFactory.findById(groupId);
    User user = getSelf();
    if (group == null || !group.getOwnerId().equals(user.getId())) {
      return ResponseModel.buildParameterError();
    }
    PushFactory.pushGroupDelete(group);

    GroupFactory.deleteGroup(group);
    return ResponseModel.buildOk();
  }

  // 获取群列表
  @GET
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<List<GroupCard>> getGroupList() {
    List<GroupMember> memberList = GroupFactory.findGroupMembers(getSelf().getId());
    Set<GroupCard> groupCardSet = new HashSet<>();
    for (GroupMember member : memberList) {
      Group group = GroupFactory.findById(member.getGroupId());
      if (group != null) {
        groupCardSet.add(new GroupCard(group, member));
      }
    }
    return ResponseModel.buildOk(new ArrayList<>(groupCardSet));
  }

  // 更新群
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<GroupCard> updateGroup(UpdateGroupModel model) {
    if (!UpdateGroupModel.check(model)) {
      return ResponseModel.buildParameterError();
    }
    User user = getSelf();
    Group group = GroupFactory.findById(model.getGroupId());
    if (group == null || !group.getOwnerId().equals(user.getId())) {
      return ResponseModel.buildParameterError();
    }
    group = model.update(group);
    group = GroupFactory.updateGroup(group);
    if (group != null) {
      GroupMember groupMember = GroupFactory.findOwnerMember(group);
      if (groupMember != null) {
        GroupCard groupCard = new GroupCard(group, groupMember);
        return ResponseModel.buildOk(groupCard);
      }
    }
    return ResponseModel.buildRegisterError();
  }

  // 增加群成员
  @POST
  @Path("/member")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<List<GroupMemberCard>> addGroupMember(List<GroupMemberCreateModel> models) {
    Group group = GroupFactory.findById(getSelf(), models.get(0).getGroupId());
    for (GroupMemberCreateModel model : models) {
      if (!GroupMemberCreateModel.check(model)) {
        return ResponseModel.buildParameterError();
      }
      User user = UserFactory.findById(model.getUserId());
      if (user == null || group == null) {
        return ResponseModel.buildParameterError();
      }
      GroupMember member1 = GroupFactory.findMember(group.getId(), user.getId());
      if (member1 != null) {
        return ResponseModel.buildParameterError();
      }
      GroupMember groupMember = GroupFactory.createGroupMember(group, user, false);
      if (groupMember != null) {
        PushFactory.pushGroupMember(PushModel.ENTITY_TYPE_ADD_GROUP, user, groupMember);
      } else {
        return ResponseModel.buildServiceError();
      }
    }
    List<GroupMemberCard> cardList = new LinkedList<>();
    Set<GroupMember> groupMembers = GroupFactory.getMembers(group);
    for (GroupMember member : groupMembers) {
      GroupMemberCard card = new GroupMemberCard(member);
      cardList.add(card);
    }
    return ResponseModel.buildOk(cardList);
  }

  //删除群成员
  @PUT
  @Path("/member")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<List<GroupMemberCard>> deleteGroupMember(
      List<GroupMemberCreateModel> models) {
    Group group = GroupFactory.findById(getSelf(), models.get(0).getGroupId());
    for (GroupMemberCreateModel model : models) {
      if (!GroupMemberCreateModel.check(model)) {
        return ResponseModel.buildParameterError();
      }
      User user = UserFactory.findById(model.getUserId());
      if (user == null || group == null) {
        return ResponseModel.buildParameterError();
      }
      if (user.getId().equals(getSelf().getId())) {
        continue;
      }
      GroupMember groupMember = GroupFactory.findMember(group.getId(), user.getId());
      PushFactory.pushGroupMember(PushModel.ENTITY_TYPE_DELETE_GROUP, user, groupMember);
      GroupFactory.deleteGroupMember(group, user);
    }
    List<GroupMemberCard> cardList = new LinkedList<>();
    Set<GroupMember> groupMembers = GroupFactory.getMembers(group);
    for (GroupMember member : groupMembers) {
      GroupMemberCard card = new GroupMemberCard(member);
      cardList.add(card);
    }
    return ResponseModel.buildOk(cardList);
  }

  @GET
  @Path("/member/{groupId}")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<List<GroupMemberCard>> getGroupMember(@PathParam("groupId") String groupId) {
    Group group = GroupFactory.findById(groupId);
    if (group == null) {
      return ResponseModel.buildParameterError();
    }
    List<GroupMemberCard> cardList = new LinkedList<>();
    Set<GroupMember> groupMembers = GroupFactory.getMembers(group);
    for (GroupMember member : groupMembers) {
      GroupMemberCard card = new GroupMemberCard(member);
      cardList.add(card);
    }
    return ResponseModel.buildOk(cardList);
  }
}
